//
//  MyTypePageController.m
//  mosad_hw_fin
//
//  Created by wyw on 2021/1/4.
//  Copyright © 2021 student9. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "MyTypePageController.h"
#import "../view/ImageButton.h"
@interface MyTypePageController ()<UITableViewDelegate, UITableViewDataSource>

@property NSInteger showingSection;
@property (nonatomic, strong) UIView *notlogin_pic;
@property (nonatomic, strong) UILabel *notlogin_label;

@end

@implementation MyTypePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.showingSection = -1;
    
    __weak typeof(self) weakSelf = self;
    self.view.backgroundColor = [UIColor colorWithRed:230/255.0 green:230/255.0  blue:250/255.0 alpha:1];
    self.navigationController.navigationBar.hidden = YES;
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
    UIView *backgroundMine = [[UIView alloc]  initWithFrame:CGRectMake(0, 0, weakSelf.view.frame.size.width, weakSelf.view.frame.size.height / 2.4)];
    backgroundMine.backgroundColor = [UIColor colorWithPatternImage:[[UIImage imageNamed:@"backgroundMine.png" ]TransformtoSize:CGSizeMake(weakSelf.view.frame.size.width, weakSelf.view.frame.size.height / 2.4)]] ;
    [self.view addSubview:backgroundMine];
    
    UIView *borderMine = [[UIView alloc] initWithFrame: CGRectMake(0, 0, weakSelf.view.frame.size.width - 60, weakSelf.view.frame.size.height - 200)];
    borderMine.backgroundColor = [UIColor whiteColor];
    borderMine.layer.shadowColor = [UIColor blackColor].CGColor;
    borderMine.layer.shadowOpacity = 1;
    borderMine.layer.shadowRadius = 40;
    borderMine.layer.shadowOffset = CGSizeMake(70, 70);
    UIBezierPath * maskPath_borderMine = [UIBezierPath bezierPathWithRoundedRect:borderMine.bounds cornerRadius:40];
    CAShapeLayer * maskLayer_borderMine = [[CAShapeLayer alloc] init];
    maskLayer_borderMine.frame = borderMine.bounds;
    maskLayer_borderMine.path = maskPath_borderMine.CGPath;
    borderMine.layer.mask = maskLayer_borderMine;
    borderMine.layer.shouldRasterize = YES;
    borderMine.layer.masksToBounds = NO;
    borderMine.clipsToBounds = NO;
    [self.view addSubview:borderMine];
    [borderMine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(weakSelf.view.frame.size.width - 60, weakSelf.view.frame.size.height - 200));
        make.centerX.equalTo(weakSelf.view.mas_centerX);
        make.centerY.equalTo(weakSelf.view.mas_centerY);
    }];
    
    UIButton *back_btn;
    back_btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    back_btn.frame = CGRectMake(0, 0, 40, 40);
    back_btn.backgroundColor = [UIColor colorWithPatternImage:[[UIImage imageNamed:@"leftarrow.png"] TransformtoSize:CGSizeMake(40, 40)]];
    [back_btn addTarget:self
                 action:@selector(back_to_mine:)
       forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:back_btn];
    [back_btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.view).offset(30);
        make.left.equalTo(weakSelf.view).offset(20);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    _notlogin_pic = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
    _notlogin_pic.backgroundColor = [UIColor colorWithPatternImage:[[UIImage imageNamed:@"warning.png"] TransformtoSize:CGSizeMake(200, 200)]];
    [self.view addSubview:_notlogin_pic];
    [_notlogin_pic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(200, 200));
        make.centerX.mas_equalTo(borderMine.mas_centerX);
        make.centerY.mas_equalTo(borderMine.mas_centerY);
    }];
    
    _notlogin_label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
    _notlogin_label.text = @"请先登录！";
    
    _notlogin_label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_notlogin_label];
    [_notlogin_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(200, 40));
        make.centerX.mas_equalTo(borderMine.mas_centerX);
        make.top.mas_equalTo(self->_notlogin_pic.mas_bottom).offset(20);
    }];
    
    headimageModel *current_head_image = [headimageModel shareInstance];
    if ([current_head_image.ID  isEqualToString: @"notlogin"] == YES) {
        _notlogin_label.layer.hidden = NO;
        _notlogin_pic.layer.hidden = NO;
    } else {
        _notlogin_label.layer.hidden = YES;
        _notlogin_pic.layer.hidden = YES;
        UITableView *Mytable = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        Mytable.delegate = self;
        Mytable.dataSource = self;
        Mytable.scrollEnabled = false;
        [self.view addSubview:Mytable];
        UIEdgeInsets edge = UIEdgeInsetsMake(25, 10, 25, 10);
        [Mytable mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(borderMine).with.insets(edge);
        }];
        self.table = Mytable;
        NSString *cellID = @"cellID";
        
        
        CellContainTableView * cell1 = [[CellContainTableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell1.Name = @"Weekly";
        cell1.UserID = current_head_image.ID;
        [cell1 initTable];
        CellContainTableView *cell2 = [[CellContainTableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell2.Name = @"Daily";
        cell2.UserID = current_head_image.ID;
        [cell2 initTable];
        CellContainTableView *cell3 = [[CellContainTableView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell3.Name = @"Onetime";
        cell3.UserID = current_head_image.ID;
        [cell3 initTable];
        
        
        self.cell1 = cell1;
        self.cell2 = cell2;
        self.cell3 = cell3;
    }
}

// 一个section基本大小cell+footer == 140；
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.showingSection == indexPath.section){
        return 3*80;
    }
    return 80;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    ImageButton * button = [[ImageButton alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    UIImage * img;
    if (self.showingSection == section){
        img = [UIImage imageNamed:@"up.png"];
    }else{
        img = [UIImage imageNamed:@"down.png"];
    }
    [button addImage:img];
    [button addTarget:self action:@selector(Touch:) forControlEvents:UIControlEventTouchDown];
    [button setBackgroundColor:[UIColor whiteColor]];
    button.tag = section;
    [view addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(view);
        make.centerX.equalTo(view);
    }];
    //    [view setBackgroundColor:[UIColor purpleColor]];
    return view;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 30)];
    UIImage * img = [UIImage imageNamed:@"rili.png"];
    UIImageView * image = [[UIImageView alloc] initWithImage:img];
    [view addSubview:image];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 60, 20)];
    switch (section) {
        case 0:
            [label setText:@"周常事件"];
            break;
        case 1:
            [label setText:@"日常事件"];
            break;
        case 2:
            [label setText:@"单次事件"];
            break;
        default:
            break;
    }
    [view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(image);
        make.left.equalTo(image.mas_right).offset(10);
    }];
    //    [view setBackgroundColor:[UIColor greenColor]];
    return view;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.showingSection == -1 || self.showingSection == section){
        return 1;
    }
    return 0;
}

- (CellContainTableView *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellContainTableView *cell;
    switch (indexPath.section) {
        case 0:
            cell = self.cell1;
            break;
        case 1:
            cell = self.cell2;
            break;
        case 2:
            cell = self.cell3;
            break;
        default:
            break;
    }
    cell.isshowingsection = (self.showingSection == indexPath.section);
    return cell;
}


#pragma button_event_Handler

-(void) Touch: (UIButton *) sender{
    if (self.showingSection == sender.tag){
        switch (sender.tag) {
            case 0:
                self.cell1.isshowingsection = false;
                [self.cell1.table reloadData];
                break;
            case 1:
                self.cell2.isshowingsection = false;
                [self.cell2.table reloadData];
                break;
            case 2:
                self.cell3.isshowingsection = false;
                [self.cell3.table reloadData];
                break;
            default:
                break;
        }
        self.showingSection = -1;
    }else{
        if (self.showingSection != -1){
            switch (self.showingSection) {
                case 0:
                    self.cell1.isshowingsection = false;
                    [self.cell1.table reloadData];
                    break;
                case 1:
                    self.cell2.isshowingsection = false;
                    [self.cell2.table reloadData];
                    break;
                case 2:
                    self.cell3.isshowingsection = false;
                    [self.cell3.table reloadData];
                    break;
                default:
                    break;
            }
        }
        self.showingSection = sender.tag;
        switch (sender.tag) {
            case 0:
                self.cell1.isshowingsection = true;
                [self.cell1.table reloadData];
                break;
            case 1:
                self.cell2.isshowingsection = true;
                [self.cell2.table reloadData];
                break;
            case 2:
                self.cell3.isshowingsection = true;
                [self.cell3.table reloadData];
                break;
            default:
                break;
        }
    }
    [self.table reloadData];
    NSLog(@"showing %ld",(long)self.showingSection);
}

- (void)back_to_mine:(UIButton *)buttonView {
    NSLog(@"back_to_mine");
    [self.navigationController popViewControllerAnimated:NO];
}

@end

